#include <gtest/gtest.h>
#include <vanetza/security/backend.hpp>
#include <vanetza/security/ecc_point.hpp>
#include <memory>

using namespace vanetza::security;
using namespace vanetza;

class EccPointDecompressionTest: public ::testing::TestWithParam<const char*>
{
protected:
    std::unique_ptr<Backend> backend;

    void SetUp() override
    {
        backend = create_backend(GetParam());
        ASSERT_TRUE(backend);
    }
};


TEST_P(EccPointDecompressionTest, LSB_Y_0)
{
    const ByteBuffer x = {
        0x21, 0x31, 0xB5, 0x19, 0x4C, 0xE6, 0xEE, 0x9F,
        0x47, 0xB6, 0xB4, 0x5F, 0xF8, 0x46, 0xC1, 0x79,
        0x65, 0x1A, 0x4A, 0x15, 0x63, 0x5A, 0x09, 0x87,
        0xCC, 0xC3, 0x8F, 0x3E, 0x34, 0x4D, 0xCD, 0x77 };
    const ByteBuffer y_expected = {
        0xD0, 0x12, 0x39, 0xBF, 0x92, 0x7F, 0x16, 0xA3,
        0xAD, 0xDB, 0x58, 0x2F, 0x94, 0x9B, 0xA7, 0x25,
        0x8F, 0xAC, 0x53, 0xFE, 0xD2, 0xE7, 0x26, 0xDB,
        0x9C, 0x62, 0xC3, 0x2C, 0xAC, 0x8D, 0xBA, 0x5A };
    const EccPoint point = Compressed_Lsb_Y_0 { x };

    auto decompressed = backend->decompress_point(point);
    ASSERT_TRUE(decompressed);
    EXPECT_EQ(x, decompressed->x);
    EXPECT_EQ(y_expected, decompressed->y);
}

TEST_P(EccPointDecompressionTest, LSB_Y_1)
{
    const ByteBuffer x = {
        0x7F, 0x92, 0xF5, 0xBA, 0x25, 0xE0, 0x2C, 0x7F,
        0x7C, 0xF9, 0x82, 0x2D, 0x74, 0x6F, 0x28, 0x05,
        0x65, 0xD6, 0xA1, 0x4A, 0x64, 0x38, 0x40, 0x61,
        0x2E, 0x08, 0x63, 0x4D, 0x6E, 0x85, 0x7F, 0x13 };
    const ByteBuffer y_expected = {
        0x32, 0xD9, 0x85, 0xB6, 0x1D, 0x0E, 0x70, 0x51,
        0x51, 0x03, 0x88, 0xF9, 0x08, 0x2E, 0x24, 0x05,
        0x0B, 0x1A, 0xFE, 0xB6, 0x56, 0x49, 0x2D, 0x17,
        0x29, 0xB2, 0x8F, 0x9B, 0x58, 0xCE, 0xCB, 0xAF };
    const EccPoint point = Compressed_Lsb_Y_1 { x };

    auto decompressed = backend->decompress_point(point);
    ASSERT_TRUE(decompressed);
    EXPECT_EQ(x, decompressed->x);
    EXPECT_EQ(y_expected, decompressed->y);
}

auto values = ::testing::Values(
#ifdef VANETZA_WITH_OPENSSL
    "OpenSSL",
#endif
    "CryptoPP"
);

INSTANTIATE_TEST_SUITE_P(EccPointDecompression, EccPointDecompressionTest, values);
